const express = require('express');
const cors = require('cors');
const path = require('path');

const app = express();
const PORT = process.env.PORT || 3000;

// 中间件
app.use(cors());
app.use(express.json());
app.use(express.static(__dirname));

// 模拟用户数据
const mockUsers = [
    { username: 'admin', password: 'admin123' },
    { username: 'user1', password: 'password123' },
    { username: 'test', password: 'test123' }
];

// 登录接口
app.post('/login', (req, res) => {
    const { username, password } = req.body;
    
    // 验证输入
    if (!username || !password) {
        return res.status(400).json({
            success: false,
            message: '用户名和密码不能为空'
        });
    }
    
    // 模拟验证延迟
    setTimeout(() => {
        const user = mockUsers.find(u => u.username === username && u.password === password);
        
        if (user) {
            res.json({
                success: true,
                message: '登录成功',
                user: {
                    username: user.username,
                    loginTime: new Date().toISOString()
                }
            });
        } else {
            res.status(401).json({
                success: false,
                message: '用户名或密码错误'
            });
        }
    }, 1000); // 模拟网络延迟
});

// 提供静态文件服务
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'index.html'));
});

// 启动服务器
app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
    console.log('可用的测试账号：');
    console.log('- 用户名: admin, 密码: admin123');
    console.log('- 用户名: user1, 密码: password123');
    console.log('- 用户名: test, 密码: test123');
});
